package com.xing.photo.funcation;

import com.xing.photo.util.StaticUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/3/2
 * Time: 17:21
 */
public class Uid {
    private static Logger photoPath = LoggerFactory.getLogger("photoPath");
    public static void main(String[] args) throws Exception {
        Pattern pattern = Pattern.compile("\"uid=(.*?)\\&");
        Set<String> set = new HashSet<String>();
        for (int i = 1; i < 8; i++) {
            URI uri = new URI("http://weibo.com/p/1005052651069287/myfollow?t=1&pids=Pl_Official_RelationMyfollow__102&cfs=&Pl_Official_RelationMyfollow__102_page=" + i + "&ajaxpagelet=1&ajaxpagelet_v6=1&__ref=/2651069287/follow&_t=FM_142552595525539");
            String content = getContent(uri);
            Matcher m = pattern.matcher(content);
            while (m.find()) {
                String uid = m.group(1);
                if (uid.length() < 21) {
                    set.add(uid);
                }
            }
        }
        System.out.println(set);
        System.out.println(set.size());
        photoPath.info("set: " + set);
    }

    private static String getContent(URI uri) {
        String result = null;
        try {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Cookie", StaticUtil.cookies);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity re = response.getEntity();
            result = EntityUtils.toString(re, "gbk");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
